/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * 千分位分隔符
 */
export function numFormat(num) {
  var res = num.toString().replace(/\d+/, function(n) { // 先提取整数部分
    return n.replace(/(\d)(?=(\d{3})+$)/g, function($1) {
      return $1 + ",";
    });
  })
  return res;
}
//获取#号值
export function GetRequest() {
  var str = location.href
  var num = str.indexOf("#");
  str = str.substr(num + 1);
  return str;
}
export function getCaption(obj) {
  var index = obj.lastIndexOf("\/");
  obj = obj.substring(index + 1, obj.length);
  //  console.log(obj);
  return obj;
}
//时间格式化 yyyy-MM-dd HH:mm:ss
export function formatTime(date, fmt) {
  var o = {
    "M+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时
    "H+": date.getHours(), //小时
    "m+": date.getMinutes(), //分
    "s+": date.getSeconds(), //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    "S": date.getMilliseconds() //毫秒
  };
  var week = {
    "0": "/u65e5",
    "1": "/u4e00",
    "2": "/u4e8c",
    "3": "/u4e09",
    "4": "/u56db",
    "5": "/u4e94",
    "6": "/u516d"
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  if (/(E+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") +
      week[date.getDay() + ""]);
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
}
//获取当前系统时间（包括星期）
export function getCurrentDate() {
  var myDate = new Date();
  var year = myDate.getFullYear(); //年
  var month = myDate.getMonth() + 1; //月
  var day = myDate.getDate(); //日
  var days = myDate.getDay();
  var hour = myDate.getHours();
  var minutes = myDate.getMinutes();
  var seconds = myDate.getSeconds();
  if (day < 10) {
    day = '0' + day
  }
  if (month < 10) {
    month = '0' + month
  }
  if (hour < 10) {
    hour = '0' + hour
  }
  if (minutes < 10) {
    minutes = '0' + minutes
  }
  if (seconds < 10) {
    seconds = '0' + seconds
  }
  switch (days) {
    case 1:
      days = '星期一';
      break;
    case 2:
      days = '星期二';
      break;
    case 3:
      days = '星期三';
      break;
    case 4:
      days = '星期四';
      break;
    case 5:
      days = '星期五';
      break;
    case 6:
      days = '星期六';
      break;
    case 0:
      days = '星期日';
      break;
  }
  var str = year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + seconds + ' ' + days;
  return str;
}

//获取当前系统时间（到月份）
export function getCurrentMonth() {
  var myDate = new Date();
  var year = myDate.getFullYear(); //年
  var month = myDate.getMonth() + 1; //月

  if (month < 10) {
    month = '0' + month
  }

  var str = year + "-" + month;
  return str;
}

//定时刷新页面接口数据
export function getTimer(obj, fun) {
  if (obj) {
    clearInterval(obj)
  } else {
    obj = setInterval(() => {
      setTimeout(fun(), 0)
      // fun()
    }, timerTime)
  }
  return obj
}
//js读取excel中日期格式转换问题
export function formatDate(numb, format) {
  const time = new Date((numb - 1) * 24 * 3600000 + 1)
  time.setYear(time.getFullYear() - 70)
  const year = time.getFullYear() + ''
  const month = time.getMonth() + 1 + ''
  const date = time.getDate() - 1 + ''
  if (format && format.length === 1) {
    return year + format + month + format + date
  }
  return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
}
/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}
/**获取最新最上最下行 */
export function getTopAndBottom(row, bottom, top) {
  let n = row.rowIndex,
    mx = bottom.rowIndex,
    mi = top.rowIndex;
  if (n > mx) {
    return {
      top: mi,
      bottom: n
    };
  } else if (n < mx && n > mi) {
    return {
      top: mi,
      bottom: n
    };
  } else if (n < mi) {
    return {
      top: n,
      bottom: mx
    };
  } else if (n == mi || n == mx) {
    return {
      top: mi,
      bottom: mx
    };
  }
};
//js截取指定字符前面或后面的内容
export function getString(obj, state) {
  var index = obj.lastIndexOf("\-");
  if (state == 0) {
    obj = obj.substring(0, index);
  } else {
    obj = obj.substring(index + 1, obj.length);
  }
  return obj;
}
